//
//  RefundDetailViewController.m
//  BaseProject
//
//  Created by 王洋 on 2020/9/17.
//  Copyright © 2020 王洋. All rights reserved.
//
#import "RefundDetailInfoCell.h"
#import "RefundCredentialsCell.h"
#import "RefundDetailStatusCell.h"
#import "RefundFlowStepCell.h"
#import "SerOrderDetailStatusCell.h"

#import "RefundDetailBottomView.h"
#import "ApplyRefundViewController.h"
#import "RefundDetailViewController.h"

@interface RefundDetailViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableview;

@property (nonatomic, strong) CommonRefundDetailModel *model;

@property (nonatomic, strong) RefundDetailBottomView *bottomView;

@end

@implementation RefundDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self request_handleRefund];
}

- (void)initView {

    self.title = @"退款详情";
    
    self.bottomView =  [[NSBundle mainBundle] loadNibNamed:@"RefundDetailBottomView"owner:nil options:nil].firstObject;
    
    //状态（1：已申请，2：商家同意，3：商家拒绝，4：平台同意，5：平台拒绝，6：退款中，7：退款成功，8：退款失败）
    if (self.model.refundStatus == 3 || self.model.refundStatus == 5) {
        [self.view addSubview:self.bottomView];
        [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.bottom.right.mas_equalTo(0);
            make.height.mas_equalTo(Bottom_SafeHeight + 60);
        }];
        
        [self.view addSubview:self.tableview];
        [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.right.mas_equalTo(0);
            make.bottom.mas_equalTo(self.bottomView.mas_top);
        }];
        
        WEAKSELF
        self.bottomView.RefundDetailBottomViewBlock = ^{
            [weakSelf goApplyRefund];
        };
        
    } else {
        [self.view addSubview:self.tableview];
        [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.right.mas_equalTo(0);
            make.bottom.mas_equalTo(0);
        }];
    }
    
    [self.tableview reloadData];

}

- (void)goApplyRefund {
    
    ApplyRefundViewController *vc = [[ApplyRefundViewController alloc] init];
    vc.paramModel = [[ApplyRefundParamModel alloc] init];
    vc.paramModel.orderNo = self.model.orderNo;
    vc.paramModel.payAmount = self.model.refundAmount;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark UITableviewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section == 0) {
        return 6;
    } else {
        return self.model.userRefundFlowList.count;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    switch (indexPath.section) {
        case 0:{
            
            switch (indexPath.row) {
                case RefundDetailCellType_status:{
                    return 50;
                } break;
                case RefundDetailCellType_money:{
                    return 50;
                } break;
                case RefundDetailCellType_reason:{
                    return 50;
                } break;
                case RefundDetailCellType_userReason:{
                    
                    CGFloat height = [Utilities getStringHeightWithText:self.model.remark font:[UIFont systemFontOfSize:12] viewWidth:(TTScreenWidth - 105)] + 20;
                    return height;
                    
                } break;
                case RefundDetailCellType_pingZheng:{
                    return 120;
                } break;
                case RefundDetailCellType_flowStep:{
                    return 40;
                } break;
                default:
                    break;
            }
        } break;
        case 1:{
            
            return 40;
        } break;
        default:
            break;
    }
    return CGFLOAT_MAX;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    switch (indexPath.section) {
        case 0:{
            
            switch (indexPath.row) {
                case RefundDetailCellType_status:{
                    SerOrderDetailStatusCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SerOrderDetailStatusCellID"];
                    cell.selectionStyle =UITableViewCellSelectionStyleNone;
                    [cell fillCellWithRFModel:self.model];
                    
                    return cell;
                } break;
                case RefundDetailCellType_money:{
                    RefundDetailInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RefundDetailInfoCellID"];
                    cell.selectionStyle =UITableViewCellSelectionStyleNone;
                    cell.mainTitleLab.text = @"退款金额：";
                    cell.subTitleLab.textColor = UIColorHex(0xff2442);
                    cell.subTitleLab.text = [NSString stringWithFormat:@"¥%@",[Utilities changeDoubleToString:self.model.userRefundAmount]];

                    return cell;
                } break;
                case RefundDetailCellType_reason:{
                    RefundDetailInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RefundDetailInfoCellID"];
                    cell.selectionStyle =UITableViewCellSelectionStyleNone;
                    cell.mainTitleLab.text = @"具体原因：";
                    cell.subTitleLab.text = self.model.refundReasonText;

                    return cell;
                } break;
                    case RefundDetailCellType_userReason:{

                        RefundDetailInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RefundDetailInfoCellID"];
                        cell.selectionStyle =UITableViewCellSelectionStyleNone;
                        cell.mainTitleLab.text = @"具体说明：";
                        cell.subTitleLab.text = self.model.remark;

                        return cell;
                    } break;
                case RefundDetailCellType_pingZheng:{
                    RefundCredentialsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RefundCredentialsCellID"];
                    cell.selectionStyle =UITableViewCellSelectionStyleNone;
                    [cell fillCellWithModel:self.model];
                    return cell;
                } break;
                case RefundDetailCellType_flowStep:{
                    RefundDetailStatusCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RefundDetailStatusCellID"];
                    cell.selectionStyle =UITableViewCellSelectionStyleNone;
                    return cell;
                } break;
                default:
                    break;
            }
            
        } break;
        case 1:{
            
            RefundFlowStepCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RefundFlowStepCellID"];
            cell.selectionStyle =UITableViewCellSelectionStyleNone;
            [cell fillCellWithModel:self.model.userRefundFlowList[indexPath.row] index:indexPath.row];
            return cell;
            
        } break;
        default:
            break;
    }
        
    RefundDetailInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RefundDetailInfoCellID"];
    cell.selectionStyle =UITableViewCellSelectionStyleNone;
    return cell;
}

- (UITableView *)tableview {
    if (!_tableview) {
        _tableview = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableview.backgroundColor =  UIColorHex(0xf1f1f1);
        
        _tableview.showsHorizontalScrollIndicator = NO;
        _tableview.showsVerticalScrollIndicator = NO;
        
        _tableview.estimatedSectionHeaderHeight = 0;
        _tableview.estimatedSectionFooterHeight = 0;
        _tableview.estimatedRowHeight = 0;
        
        
        [_tableview registerNib:[UINib nibWithNibName:@"SerOrderDetailStatusCell" bundle:nil] forCellReuseIdentifier:@"SerOrderDetailStatusCellID"];

        
        [_tableview registerNib:[UINib nibWithNibName:@"RefundDetailInfoCell" bundle:nil] forCellReuseIdentifier:@"RefundDetailInfoCellID"];
        
        [_tableview registerNib:[UINib nibWithNibName:@"RefundCredentialsCell" bundle:nil] forCellReuseIdentifier:@"RefundCredentialsCellID"];

        [_tableview registerNib:[UINib nibWithNibName:@"RefundDetailStatusCell" bundle:nil] forCellReuseIdentifier:@"RefundDetailStatusCellID"];
        
        [_tableview registerNib:[UINib nibWithNibName:@"RefundFlowStepCell" bundle:nil] forCellReuseIdentifier:@"RefundFlowStepCellID"];

    }
    return _tableview;
}

//
- (void)request_handleRefund {
    
    NSMutableDictionary *dic = @{}.mutableCopy;
    [dic setValue:self.rfId forKey:@"rfId"];
    [dic setValue:self.uosType forKey:@"uosType"];
    [dic setValue:self.uosOrderNo forKey:@"uosOrderNo"];
    [dic setValue:@"1" forKey:@"type"];

    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,API_RefundDetail];

    WEAKSELF
    [SVProgressHUD showWithStatus:@"正在请求退款详情"];
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {

        [SVProgressHUD dismiss];
        if ([data isKindOfClass:[NSDictionary class]]) {
            //请求成功
            NSInteger code = [[data valueForKey:@"code"] integerValue];
            if (code == 10000) {

                NSDictionary *resultDic = [data valueForKey:@"data"];
                if (![resultDic isKindOfClass:[NSDictionary class]]) {
                    return;
                }
                NSArray *sysMediaList = [resultDic valueForKey:@"sysMediaList"];
                NSArray *userRefundFlowList = [resultDic valueForKey:@"userRefundFlowList"];
                weakSelf.model = [CommonRefundDetailModel modelWithDictionary:resultDic];

                if ([sysMediaList isKindOfClass:[NSArray class]]) {
                    weakSelf.model.sysMediaList = @[].mutableCopy;
                    
                    for (int i = 0; i < sysMediaList.count; i++) {
                        CommonMediaModel *model = [CommonMediaModel modelWithDictionary:sysMediaList[i]];
                        [weakSelf.model.sysMediaList addObject:model];
                    }
                }
                
                if ([userRefundFlowList isKindOfClass:[NSArray class]]) {
                    weakSelf.model.userRefundFlowList = @[].mutableCopy;
                    
                    for (int i = 0; i < userRefundFlowList.count; i++) {
                        RefundFlowModel *model = [RefundFlowModel modelWithDictionary:userRefundFlowList[i]];
                        [weakSelf.model.userRefundFlowList addObject:model];
                    }
                }
                
                [weakSelf initView];
                
            } else {

                [SVProgressHUD showErrorWithStatus:[data valueForKey:@"message"]];
                [SVProgressHUD dismissWithDelay:1];
            }
        }

    } FailedBlock:^(NSError * _Nonnull error) {
        [SVProgressHUD dismiss];

    }];
}

@end

